﻿
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Command
{
    public class EditAddDeviceCommand : IRequest<Json<string>>
    {
        public int id { get; set; }
        /// <summary>
        /// 设备编码
        /// </summary>
        public string deviceid { get; set; }
        /// <summary>
        /// 自定义设备编码
        /// </summary>
        public string cusdeviceid { get; set; }
        /// <summary>
        /// 产品id
        /// </summary>
        public int skuid { get; set; }
        /// <summary>
        /// 设备最大容量
        /// </summary>
        public int maxcapacity { get; set; }
        /// <summary>
        /// 设备容量阈值
        /// </summary>
        public int capacitythreshold { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }
    }

    public class EditAddDeviceCommandHandler : IRequestHandler<EditAddDeviceCommand, Json<string>>
    {
        private readonly IDevicetableRepository _idevicetableRepository;
        private readonly ICurrentUserService _icurrentUserServices;
        public EditAddDeviceCommandHandler(IDevicetableRepository idevicetableRepository, ICurrentUserService icurrentUserServices)
        {
            _idevicetableRepository = idevicetableRepository;
            _icurrentUserServices = icurrentUserServices;
        }
        public async Task<Json<string>> Handle(EditAddDeviceCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            if (userid <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请先登录" };
            }
            else if (request.id <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请选择需要修改的设备" };
            }
            else if (string.IsNullOrWhiteSpace(request.deviceid))
            {
                return new Json<string>() { Status = 1003, Detail = "设备编码不能为空" };
            }
            else
            {
                var model = await _idevicetableRepository.GetDevicetableByid(request.id, cancellationToken);
                if ((model?.id ?? 0) <= 0)
                {
                    return new Json<string>() { Status = 1003, Detail = "请选择有效的需要修改的设备" };
                }
                else if (request.deviceid != model.deviceid)
                {
                    return new Json<string>() { Status = 1003, Detail = "设备编码暂时不支持修改" };
                }
                else
                {
                    //校验设备编码
                    model.deviceid = request.deviceid;
                    model.cusdeviceid = request.cusdeviceid;
                    model.skuid = request.skuid;
                    model.capacitythreshold = request.capacitythreshold;
                    model.maxcapacity = request.maxcapacity;
                    model.updatetime = DateTime.Now;
                    model.userpid = userid;
                    model.remark = request.remark;
                    await _idevicetableRepository.EditDeviceTable(model, cancellationToken);

                    return new Json<string>() { Status = 1000, Detail = "修改成功" };
                }
            }
        }
    }
}
